//
//  FavoriteList.m
//  Pods
//
//  Created by E.F.B on 15-5-26.
//
//

#import "FavoriteList.h"
#import "TCAirportModel.h"

@interface FavoriteList ()

@property (nonatomic, retain) UITableView  *theFavoriteTable;

@property (nonatomic, retain) NSMutableDictionary *dicFavCharts;
@property (nonatomic, retain) NSMutableArray *fav_airports;

@end


@implementation FavoriteList


@synthesize theFavoriteTable = theFavoriteTable_;
@synthesize fav_airports;

#pragma mark -- life cycle
- (void)dealloc
{
    [_dicFavCharts release];
    [fav_airports release];
    [theFavoriteTable_ release];
    [super dealloc];
}

- (id)init
{
    self=[super init];
    if (self) {
        _dicFavCharts = [[NSMutableDictionary alloc] init];
        fav_airports = [[NSMutableArray alloc] init];
    }
    
    return self;
}


- (void)viewDidLoad
{
    UINavigationBar *navigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]autorelease];
    [navigationBar setBarTintColor:[UIColor colorWithRed:48/255.0 green:62/255.0 blue:75/255.0 alpha:1.0]];
    UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:NSLocalizedString(@"favorite-airport", @"收藏机场")]autorelease];
    [navigationBar setItems:@[item]];
    navigationBar.translucent = NO;
    UIColor *cc = [UIColor whiteColor];
    NSDictionary * dict = [NSDictionary dictionaryWithObject:cc forKey:NSForegroundColorAttributeName];
    navigationBar.titleTextAttributes = dict;

    [self.view addSubview:navigationBar];
    
    self.theFavoriteTable=[[[UITableView alloc]initWithFrame:CGRectMake(0, 44, 320,400-44) style:UITableViewStylePlain]autorelease];
    self.theFavoriteTable.delegate=self;
    self.theFavoriteTable.dataSource=self;
    self.theFavoriteTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.theFavoriteTable.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1.0];
    [self.view addSubview:theFavoriteTable_];
    [self.view setBackgroundColor:[UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1.0]];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.dicFavCharts = [TCAirportModel getFavorateDic];
    if (self.dicFavCharts) {
        self.fav_airports = [NSMutableArray arrayWithArray:[self.dicFavCharts allKeys]];
    }
    [self.theFavoriteTable reloadData];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidUnload{
    self.theFavoriteTable = nil;
    [super viewDidUnload];
}

#pragma mark - tableview datasource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.fav_airports count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *favoriteIdentifier=@"favoriteIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:favoriteIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:favoriteIdentifier] autorelease];
    }
    
    NSString *airport = [self.fav_airports objectAtIndex:indexPath.row];
    cell.textLabel.text = airport;
    
    NSDictionary *dic = [self.dicFavCharts objectForKey:airport];
    NSString *message = @"";
    for (NSString *string in [dic allKeys]) {
        NSArray *array = [dic objectForKey:string];
        NSString *tmp = [string isEqualToString:@"STR"] ? @"STAR" : string;
        message = [NSString stringWithFormat:@"%@%@:%ld ",message,tmp,(unsigned long)[array count]];
    }
    cell.detailTextLabel.text = message;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSUInteger row = [indexPath row];
        NSString *selectedICAO = [self.fav_airports objectAtIndex:row];
        
        [TCAirportModel removeFavorateDicForIcao:selectedICAO];

        [self.dicFavCharts removeObjectForKey:selectedICAO];
        [self.fav_airports removeObject:selectedICAO];
        
        if ([selectedICAO isEqualToString:[TCAirportModel getAirportICAO]]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"EFBFavoriteNotificationClear" object:selectedICAO];
        }
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.theFavoriteTable reloadData];
        });
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return NSLocalizedString(@"Unbundling", @"取消关注");
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row=[indexPath row];
    NSString *selectedICAO = [self.fav_airports objectAtIndex:row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"selectedAirportOfChartsChanged" object:selectedICAO];
    [[NSUserDefaults standardUserDefaults] setValue:selectedICAO forKey:@"blueSiZiMa"];
}

@end
